import {Component, OnInit, ViewEncapsulation, ChangeDetectionStrategy, ViewChild} from '@angular/core';
import {SeedInfiniteScroll} from "app/shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";
import {ActivatedRoute, Params, Router} from "@angular/router";
import {TeacherService} from "@train/teacher/teacher.service";
import {AuthService} from "@shared/service/auth/auth.service";

@Component({
  selector: 'teacher-students',
  templateUrl: './teacher-students.component.html',
  styleUrls: ['./teacher-students.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class TeacherStudentsComponent implements OnInit {
  public students = [];
  public dataStore = {
    testItemId: 0,
    testType: 0,
    process: ''
  };
  public stateStore = {
    haveData: true,
    showRemarkCount: false
  };
  public paramStore = {
    page: 1,
    rows: 10,
    classId: 0,
    searchKeyword: null,
    testContentId: 0,
    orgUserId: '',
    teacherId: 0,
    beginTime: '',
    endTime: '',
  };

  @ViewChild(SeedInfiniteScroll) infiniteScroll;

  constructor(private  teacherService: TeacherService,
              private _authService: AuthService,
              private route: ActivatedRoute,
              private router: Router) {
    this.route.params
      .switchMap((params: Params) => {
        return this.route.queryParams;
      }).subscribe((queryParams: Params) => {
      this.handleRequestByDifferentProcess(queryParams);
    });
  }

  getStudentListForIdentityType() {
    if (this.dataStore.process === 'score-input') {
      this.getStudentListByClassForTeacher();
    } else {
      this.getStudentListForTeacher();
    }
  }

  /**
   * @description 根据不同流程处理请求
   * @author ouyqx-691
   * @date 2017-12-31
   */
  handleRequestByDifferentProcess(queryParams) {
    const params = this.paramStore;
    params.classId = queryParams['classId'];
    switch (queryParams['process']) {
      case  'score-input': // 成绩录入
        this.dataStore.testItemId = queryParams['testItemId'];
        this.dataStore.testType = queryParams['testType'];
        params.testContentId = queryParams['testContentId'];
        params.orgUserId = this._authService.getAuthOrgId();
        this.getStudentListByClassForTeacher();
        break;
      case 'score-query':  // 成绩查询
        params.teacherId = queryParams['teacherId'];
        params.beginTime = queryParams['beginTime'];
        params.endTime = queryParams['endTime'];

        // console.log(this.paramStore);
        this.getStudentListForTeacher();
    }
    this.dataStore.process = queryParams['process'];
  }

  /**
   * @description 根据班级信息查询学员列表（录入成绩流程）
   * @author ouyqx-691
   * @date 2017-12-31
   */
  getStudentListByClassForTeacher() {
    const {page, rows, orgUserId, classId, testContentId, searchKeyword} = this.paramStore;
    const params = {
      page: page,
      rows: rows,
      classId: classId,
      orgUserId: orgUserId,
      testItemId: testContentId,
      searchContent: searchKeyword
    };
    this.teacherService.getStudentListByClassForTeacher(params).subscribe((res) => {
      if (res.result === '0' && res.data.list) {
        const students = res.data.list;
        this.paramStore.page++;
        this.infiniteScroll.complete();
        this.students = [...this.students, ...students];
        if (!res.data.hasNextPage) {
          this.infiniteScroll.enable(false);
        }
        if (students.length === 0) {
          return this.stateStore.haveData = false;
        } else {
          return this.stateStore.haveData = true;
        }
      }
    });
  }


  /**
   * @description 实时搜索
   * @author ouyqx-691
   * @date 2017-1-4
   */
  handleKeywordsChange(keywords: string) {
    if (this.students.length === 1) { // 只有一个学生不搜索
      return;
    }
    this.students = [];
    this.paramStore.page = 1;
    this.infiniteScroll.enable(true);
    this.paramStore.searchKeyword = keywords ? keywords : null;
    this.getStudentListForIdentityType();
  }

  /**
   * @description 根据班级信息查询学员列表（查询成绩流程）
   * @author ouyqx-691
   * @date 2017-12-31
   */
  getStudentListForTeacher() {
    const {page, rows, classId, teacherId, beginTime, endTime, searchKeyword} = this.paramStore;
    const params = {
      page: page,
      rows: rows,
      classScheduleId: classId,
      search: searchKeyword,
      teacherId: teacherId,
      beginTime: beginTime,
      endTime: endTime,
    };
    this.teacherService.getStudentListForTeacher(params).subscribe((res) => {
      if (res.result === '0' && res.data.list) {
        const students = res.data.list;
        this.paramStore.page++;
        this.infiniteScroll.complete();
        this.students = [...this.students, ...students];
        if (!res.data.hasNextPage) {
          this.infiniteScroll.enable(false);
        }
        if (students.length === 0) {
          return this.stateStore.haveData = false;
        } else {
          return this.stateStore.haveData = true;
        }
      }
    });
  }

  /**
   * @description 前往成绩录入（教练）
   * @author ouyqx-691
   * @date 2018-4-13
   */
  goToTarget(student) {
    if (this.dataStore.process === 'score-input') {
      this.router.navigate([`/train/teacher/score-input`], {
        queryParams: {
          classId: this.paramStore.classId,
          testType: this.dataStore.testType,
          testItemId: this.dataStore.testItemId,
          testContentId: this.paramStore.testContentId,
          studentId: student.studentId
        }
      });
    } else {
      console.log(this._authService.getAuthOrgId());
      this.router.navigate([`/train/teacher/smart-course`], {
        queryParams: {
          studentId: student.studentId,
          idType: 'teacher',
          authOrgId: this._authService.getAuthOrgId()
        }
      });
    }
  }

  ngOnInit() {
  }
}
